<?php
	
define("IN_SYS", true);


	class ShopAction extends CommonAction{
		
		public function exchangeInfo() {
			
			$queryStr ="select u.id as uid,u.username as username,u.realname as realname,
					u.headimgurl,u.userphone as userphone,u.is_exchange as is_exchange,
					e.exchangedatetime,e.sncode,e.score,s.shopcode,s.shopname 
					from tp_exchange e join tp_user as u on e.uid=u.id join tp_shop as s on e.shopid=s.id";
			
			$Model = new Model(); // 实例化一个model对象 没有对应任何数据表
			$queryResult = $Model->query($queryStr);
			
			if($queryResult!=null){
				import('ORG.Util.Page');// 导入分页类
				$count      = count($queryResult);// 查询满足要求的总记录数
				$Page       = new Page($count,C('SHOP_PAGE_COUNT'));// 实例化分页类 传入总记录数和每页显示的记录数
				$show       = $Page->show();// 分页显示输出
				// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
				$queryStr1 ="select u.id as uid,u.username as username,u.realname as realname,
					u.headimgurl,u.userphone as userphone,u.is_exchange as is_exchange,
					e.exchangedatetime,e.sncode,e.score,s.shopcode,s.shopname 
					from tp_exchange e join tp_user as u on e.uid=u.id join tp_shop as s on e.shopid=s.id
						order by e.exchangedatetime desc limit ".$Page->firstRow.",".$Page->listRows;
				
				$list = $Model->query($queryStr1);
				$this->assign('data',$list);// 赋值数据集
				$this->assign('page',$show);// 赋值分页输出
						
			}
			
			unset($queryResult);
			$this->display();
			
		}
		
		public function searchExchange(){
			
			$where=" where 1=1";

			if(!empty($_GET['startdate'])){
			
				$where.=" and DATE_FORMAT( e.exchangedatetime,  '%Y-%m-%d' ) >='".date("Y-m-d",strtotime($_GET['startdate']))."'";
			}
				
			if(!empty($_GET['enddate'])){
				$where.=" and DATE_FORMAT( e.exchangedatetime,  '%Y-%m-%d' ) <='".date("Y-m-d",strtotime($_GET['enddate']))."'";
			}
			
			if(!empty($_GET['username'])){
				$where.=" and u.username like '%".$_GET['username']."%'";
			}
			
			if(!empty($_GET['userphone'])){
				$where.=" and u.userphone like '%".$_GET['userphone']."%'";
			}
			
			if(!empty($_GET['sncode'])){
				$where.=" and e.sncode like '%".$_GET['sncode']."%'";
			}
			
			if(!empty($_GET['shopcode'])){
				$where.=" and s.shopcode like '%".$_GET['shopcode']."%'";
			}
			
			if(!empty($_GET['shopname'])){
				$where.=" and s.shopname like '%".$_GET['shopname']."%'";
			}
			
			
			$queryStr ="select u.id as uid,u.username as username,u.realname as realname,
					u.headimgurl,u.userphone as userphone,u.is_exchange as is_exchange,
					e.exchangedatetime,e.sncode,e.score,s.shopcode,s.shopname 
					from tp_exchange e join tp_user as u on e.uid=u.id join tp_shop as s on e.shopid=s.id".$where;
			
			
			$Model = new Model(); // 实例化一个model对象 没有对应任何数据表
			
			$queryResult = $Model->query($queryStr);
			
			
			if($queryResult!=null){
				
				//带入搜索参数
				$parameter = 'userphone='.urlencode($_GET['userphone']).'&startdate='.urlencode($_GET['startdate']).'&enddate='.urlencode($_GET['enddate']).'&username='.urlencode($_GET['username']).'&userphone='.urlencode($_GET['userphone']).'&sncode='.urlencode($_GET['sncode']).'&shopcode='.urlencode($_GET['shopcode']).'&shopname='.urlencode($_GET['shopname']);
				
				import('ORG.Util.Page');// 导入分页类
				$count      = count($queryResult);// 查询满足要求的总记录数
				$Page       = new Page($count,C('SHOP_PAGE_COUNT'),$parameter);// 实例化分页类 传入总记录数和每页显示的记录数
				$show       = $Page->show();// 分页显示输出
				// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
				$where.=" order by e.exchangedatetime desc limit ".$Page->firstRow.",".$Page->listRows;
				$queryStr1 ="select u.id as uid,u.username as username,u.realname as realname,
					u.headimgurl,u.userphone as userphone,u.is_exchange as is_exchange,
					e.exchangedatetime,e.sncode,e.score,s.shopcode,s.shopname 
					from tp_exchange e join tp_user as u on e.uid=u.id join tp_shop as s on e.shopid=s.id".$where;
				
				$list = $Model->query($queryStr1);
				$this->assign('data',$list);// 赋值数据集
				$this->assign('page',$show);// 赋值分页输出
				
			}
			
			unset($queryResult);
			$this->display('exchangeInfo'); // 输出模板
		}
		
		public function repeatCheck() {
			if (!defined('IN_SYS')) {
				echo json_encode(array('status'=>'ng','msg'=>'非法操作！'));
				exit;
			}
			$shopcode = $_POST['shopcode'];
			$M = M('Shop');
			$data = $M->where("shopcode='".$shopcode."'")->find();
			if ($data && count($data) > 0) {
				echo json_encode(array('status'=>'ng','msg'=>'门店码已存在，请重新输入！'));
			} else {
				echo json_encode(array('status'=>'ok','msg'=>'门店码验证OK！'));
			}
			
			exit;
		}
		
		public function shopInfo(){
			
			$shop=M('Shop');
			
			import('ORG.Util.Page');// 导入分页类
			$count      = $shop->count();// 查询满足要求的总记录数
			$Page       = new Page($count,C('SHOP_PAGE_COUNT'));// 实例化分页类 传入总记录数和每页显示的记录数
			$show       = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list = $shop->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
			$this->assign('data',$list);// 赋值数据集
			$this->assign('page',$show);// 赋值分页输出
			$this->display(); // 输出模板
		}
		
		public function searchShop(){
			
			$map=array();
			if(!empty($_GET['shopcode'])){
				$map['shopcode'] = array('like','%'.$_GET['shopcode'].'%');
			}
			if(!empty($_GET['shopname'])){
				$map['shopname'] = array('like','%'.$_GET['shopname'].'%');
			}

			
			$parameter = 'shopcode='.urlencode($_GET['shopcode']).'&shopname='.urlencode($_GET['shopname']);
			
			$shop = M('Shop'); // 实例化shop对象
			import('ORG.Util.Page');// 导入分页类
			$count      = $shop->where($map)->count();// 查询满足要求的总记录数
			$Page       = new Page($count,C('SHOP_PAGE_COUNT'),$parameter);// 实例化分页类 传入总记录数和每页显示的记录数
			$show       = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list = $shop->where($map)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
			$this->assign('data',$list);// 赋值数据集
			$this->assign('page',$show);// 赋值分页输出
			$this->display('shopInfo'); // 输出模板
		}
		
		public function addShop(){
			
			$this->display();
		}
		
		public function doAdd(){
			
			$shop=M('Shop');
				
			$shop->create();

			$lastId=$shop->add();
			if($lastId){
				$this->success('添加门店成功','shopInfo');
			}else{
				$this->error('添加门店失败');
			}
		}
		
		public function doDel(){
			$shop = M('Shop');
			
			$id = $_GET['id'];
			
			$count = $shop->delete($id);
			if($count>0){
				$this->success('删除门店成功');
			}else {
				$this->error('删除门店失败');
			}
		}
		
		/*
		 *	显示修改页面
		* */
		public function modifyShop(){
			$id=$_GET['id'];
		
			$m=M('Shop');
			$arr=$m->find($id);
		
			$this->assign('data',$arr);
				
			$this->display();
		}
		
		public function doUpdate(){
			$m=M('Shop');
			$data['id']=$_POST['id'];
			$data['shopcode']=$_POST['shopcode'];
			$data['shopname']=$_POST['shopname'];
				
			$count=$m->save($data);
			if($count>0){
				$this->success('修改门店成功','shopInfo');
			}else{
				$this->error('修改门店失败');
			}
		}
		
		
	}
?>
